//   单选框
let sex = $('.sex-all');
sex.click(function(){
  let kuang = $('.kuang');
  kuang.toggleClass('checked')
});
//  一系列验证
let submit = $('#submit');
let input = $('input:not(:radio)');
input.keydown(function(){
    $(this).removeClass('err')
});

submit.click(async function(e){ 
    
  let err = true;
  let user = $('#user');
  let nick = $('#nick');
  let pwd = $('#pwd');
  let pwd2 = $('#pwd2');
  let email = $('#email');
  let phone = $('#phone');
  if(!/^[a-z0-9]{2,16}$/i.test(user.val())){
      user.focus();
      user.addClass('err');
  }else if($.trim(nick.val()) == '' || nick.val().length > 10){
      nick.focus();
      nick.addClass('err');
  }else if(pwd.val().length < 8){
      pwd.focus();
      pwd.addClass('err')
  }else if(pwd2.val() !== pwd.val()){
      pwd2.focus();
      pwd2.addClass('err')
  }else if(!/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(email.val())){
      email.focus();
      email.addClass('err')
  }else if( phone.val() !=='' && !/[0-9]{7,}/.test(phone.val())){
      phone.focus();
      phone.addClass('err')
  }else{
      let {ishaveuser,ishaveemail} = $.ajax({
        url: `ajax/check.php?type=1&user=${user.val()}&email=${email.val()}`,
        type: 'GET',
        dataType: 'json',
        async:false
      }).responseJSON;
      
      if(ishaveuser){

        user.addClass('err');
      }
      if(ishaveemail){
        email.addClass('err');
      }
      if(!ishaveuser && !ishaveemail){

        err = false;
      }

  }
   
  if(err){
    e ? e.preventDefault() : window.e.returnValue = false;
  }
  
});